package com.mac.intellect_flow.admin.controller.system;

import com.mac.intellect_flow.reply.PageParam;
import com.mac.intellect_flow.reply.PageResult;
import com.mac.intellect_flow.reply.R;
import com.mac.intellect_flow.sys.entity.SysConfig;
import com.mac.intellect_flow.sys.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置
 * 
 * @author mac
 */
@Tag(name = "参数管理")
@RestController
@RequestMapping("/system/config")
public class SysConfigController{
    @Autowired
    private SysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @Operation(summary = "查询参数配置列表")
    @PreAuthorize("@ps.hasPermi('system:config:list')")
    @GetMapping("/list")
    public R<PageResult<SysConfig>> list(SysConfig config, PageParam pageParam) {
        return R.success(configService.queryPage(config, pageParam));
    }

/*    @PreAuthorize("@ps.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfig config)
    {
        List<SysConfig> list = configService.selectConfigList(config);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(response, list, "参数数据");
    }*/

    /**
     * 根据参数编号获取详细信息
     */
    @Operation(summary = "根据参数编号获取详细信息")
    @PreAuthorize("@ps.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public R getInfo(@PathVariable Long configId) {
        return R.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @Operation(summary = "根据参数键名查询参数值")
    @GetMapping(value = "/configKey/{configKey}")
    public R getConfigKey(@PathVariable String configKey) {
        return R.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @Operation(summary = "新增参数配置")
    @PreAuthorize("@ps.hasPermi('system:config:add')")
    @PostMapping
    public R add(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return R.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return R.success(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @Operation(summary = "修改参数配置")
    @PreAuthorize("@ps.hasPermi('system:config:edit')")
    @PutMapping
    public R edit(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return R.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return R.success(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @Operation(summary = "删除参数配置")
    @PreAuthorize("@ps.hasPermi('system:config:remove')")
    @DeleteMapping("/{configIds}")
    public R remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return R.success();
    }

    /**
     * 刷新参数缓存
     */
    @Operation(summary = "刷新参数缓存")
    @PreAuthorize("@ps.hasPermi('system:config:remove')")
    @DeleteMapping("/refreshCache")
    public R refreshCache() {
        configService.resetConfigCache();
        return R.success();
    }
}
